Trò chơi đua xe động vật trong UNITY Engine
- SortingLayerExposedEditor.cs
- Editor /
- Scripts /
- Tiled2Unity /
- Assets /
- project /
2 // from: https://gist.github.com/nickgravelyn/7460288
3 using System;
4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Reflection;
7 using System.Text;
8
9 using UnityEngine;
10 using UnityEditor;
11
12 [CustomEditor(typeof(SortingLayerExposed))]
13 public class SortingLayerExposedEditor : Editor
14 {
15 public override void OnInspectorGUI()
16 {
17 // Get the renderer from the target object
18 var renderer = (target as SortingLayerExposed).gameObject.GetComponent<Renderer>();
19
20 // If there is no renderer, we can't do anything
21 if (!renderer)
22 {
23 return;
24 }
25
26 // Expose the sorting layer name
27 //string newSortingLayerName = EditorGUILayout.TextField("Sorting Layer", renderer.sortingLayerName);
28 //if (newSortingLayerName != renderer.sortingLayerName)
29 //{
30 // Undo.RecordObject(renderer, "Edit Sorting Layer Name");
31 // renderer.sortingLayerName = newSortingLayerName;
32 // EditorUtility.SetDirty(renderer);
33 //}
34
35 // Expose the sorting layer ID
36 //int newSortingLayerId = EditorGUILayout.IntField("Sorting Layer ID", renderer.sortingLayerID);
37 //if (newSortingLayerId != renderer.sortingLayerID)
38 //{
39 // Undo.RecordObject(renderer, "Edit Sorting Layer ID");
40 // renderer.sortingLayerID = newSortingLayerId;
41 // EditorUtility.SetDirty(renderer);
42 //}
43
44 // Seanba: Use a popup that is populated with the acceptable sorting layers for the renderer
45 // Also allow the player to bring up the Tag/Layers inspector if they choose so
46 string[] sortLayerNames = GetSortingLayerNames();
47 //int[] sortLayerIds = GetSortingLayerUniqueIDs();
48 //{
49 // StringBuilder builder = new StringBuilder("Sorting Layers = ");
50 // for (int i = 0; i < sortLayerNames.Length; ++i)
51 // {
52 // builder.AppendFormat("({0} = {1},{2}) ", i, sortLayerIds[i], sortLayerNames[i]);
53 // }
54 // Debug.Log(builder.ToString());
55 //}
56
57 int sortLayerSelection = GetSortingLayerIndex(renderer, sortLayerNames);
58
59 GUIContent[] sortingLayerContexts = GetSortingLayerContexts();
60 int newSortingLayerIndex = EditorGUILayout.Popup(new GUIContent("Sorting Layer"), sortLayerSelection, sortingLayerContexts);
61 if (newSortingLayerIndex == sortingLayerContexts.Length - 1)
62 {
63 EditorApplication.ExecuteMenuItem("Edit/Project Settings/Tags and Layers");
64 }
65 else if (newSortingLayerIndex != sortLayerSelection)
66 {
67 //int newSortingLayerId = sortLayerIds[newSortingLayerIndex];
68 string newSortingLayerName = sortLayerNames[newSortingLayerIndex];
69
70 Undo.RecordObject(renderer, "Edit Sorting Layer ID");
71 renderer.sortingLayerName = newSortingLayerName;
72 //renderer.sortingLayerID = newSortingLayerId;
73
74 EditorUtility.SetDirty(renderer);
75 }
76
77 // Expose the manual sorting order within a sort layer
78 int newSortingLayerOrder = EditorGUILayout.IntField("Order in Layer", renderer.sortingOrder);
79 if (newSortingLayerOrder != renderer.sortingOrder)
80 {
81 Undo.RecordObject(renderer, "Edit Sorting Order");
82 renderer.sortingOrder = newSortingLayerOrder;
83 EditorUtility.SetDirty(renderer);
84 }
85 }
86
87 public static GUIContent[] GetSortingLayerContexts()
88 {
89 List<GUIContent> contexts = new List<GUIContent>();
90
91 foreach (string layerName in GetSortingLayerNames())
92 {
93 contexts.Add(new GUIContent(layerName));
94 }
95
96 contexts.Add(GUIContent.none);
97 contexts.Add(new GUIContent("Edit Layers..."));
98
99 return contexts.ToArray();
100 }
101
102 // Get the sorting layer names
103 public static string[] GetSortingLayerNames()
104 {
105 Type internalEditorUtilityType = typeof(UnityEditorInternal.InternalEditorUtility);
106 PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
107 return (string[])sortingLayersProperty.GetValue(null, new object[0]);
108 }
109
110 // Get the unique sorting layer IDs -- tossed this in for good measure
111 public int[] GetSortingLayerUniqueIDs()
112 {
113 Type internalEditorUtilityType = typeof(UnityEditorInternal.InternalEditorUtility);
114 PropertyInfo sortingLayerUniqueIDsProperty = internalEditorUtilityType.GetProperty("sortingLayerUniqueIDs", BindingFlags.Static | BindingFlags.NonPublic);
115 return (int[])sortingLayerUniqueIDsProperty.GetValue(null, new object[0]);
116 }
117
118 public static int GetSortingLayerIndex(Renderer renderer, string[] layerNames)
119 {
120 for (int i = 0; i < layerNames.Length; ++i)
121 {
122 if (layerNames[i] == renderer.sortingLayerName)
123 return i;
124
125 // Special case for Default, goddammit
126 if (layerNames[i] == "Default" && String.IsNullOrEmpty(renderer.sortingLayerName))
127 return i;
128 }
129
130 return 0;
131 }
132
133 public static int GetSortingLayerIdIndex(Renderer renderer, int[] layerIds)
134 {
135 for (int i = 0; i < layerIds.Length; ++i)
136 {
137 if (layerIds[i] == renderer.sortingLayerID)
138 return i;
139 }
140
141 return 0;
142 }
143
144 }
Based on code provided by: Nick Gravelyn
from: https:gist.github.comnickgravelyn7460288
Get the renderer from the target object
If there is no renderer, we can't do anything
Expose the sorting layer name
string newSortingLayerName = EditorGUILayout.TextField("Sorting Layer", renderer.sortingLayerName);
if (newSortingLayerName != renderer.sortingLayerName)
{
Undo.RecordObject(renderer, "Edit Sorting Layer Name");
renderer.sortingLayerName = newSortingLayerName;
EditorUtility.SetDirty(renderer);
}
Expose the sorting layer ID
int newSortingLayerId = EditorGUILayout.IntField("Sorting Layer ID", renderer.sortingLayerID);
if (newSortingLayerId != renderer.sortingLayerID)
{
Undo.RecordObject(renderer, "Edit Sorting Layer ID");
renderer.sortingLayerID = newSortingLayerId;
EditorUtility.SetDirty(renderer);
}
Seanba: Use a popup that is populated with the acceptable sorting layers for the renderer
Also allow the player to bring up the TagLayers inspector if they choose so
int[] sortLayerIds = GetSortingLayerUniqueIDs();
{
StringBuilder builder = new StringBuilder("Sorting Layers = ");
for (int i = 0; i < sortLayerNames.Length; ++i)
{
builder.AppendFormat("({0} = {1},{2}) ", i, sortLayerIds[i], sortLayerNames[i]);
}
Debug.Log(builder.ToString());
}
int newSortingLayerId = sortLayerIds[newSortingLayerIndex];
renderer.sortingLayerID = newSortingLayerId;
Expose the manual sorting order within a sort layer
Get the sorting layer names
Get the unique sorting layer IDs -- tossed this in for good measure
Special case for Default, goddammit